package de.playmee.dialogs;

import java.util.Set;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ScrollView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import de.playmee.R;
import de.playmee.Track;
import de.playmee.Weather.Condition;
import de.playmee.enums.Mood;
import de.playmee.enums.Season;
import de.playmee.enums.TimeOfDay;
import de.playmee.handler.MusicHandler;

public class EditMusicDialog extends EditDialog {

	private final String TAG = "EditMusicDialog";
	
	private Context context;
	private MusicHandler mHandler;
	private Track track;
	private Handler musicViewInitHandler;
	
	private Set<TimeOfDay> timesOfDay;
	private Set<Season> seasons;
	private Set<Condition> weatherConditions;
	
	public EditMusicDialog(Context context, MusicHandler mHandler, Track track,
			Handler musicViewInitHandler) {
		super(track.getMoods());
		this.context = context;
		this.mHandler = mHandler;
		this.track = track;
		this.musicViewInitHandler = musicViewInitHandler;
		this.timesOfDay = track.getTimesOfDay();
		this.seasons = track.getSeasons();
		this.weatherConditions = track.getWeatherConditions();
		create();
	}
	
	/**
	 * creates and sets the dialog
	 */
	private void create() {
		Log.i(TAG, "Creating the dialog...");
		
		ScrollView view = new ScrollView(context);
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.edit_music, view);
		
		// TODO bpm
		
		// moods
		CheckBox moodsAngryInput = (CheckBox) view.findViewById(R.id.editMoodsAngry);
		initCheckBox(moodsAngryInput, Mood.Angry);
		CheckBox moodsBoredInput = (CheckBox) view.findViewById(R.id.editMoodsBored);
		initCheckBox(moodsBoredInput, Mood.Bored);
		CheckBox moodsInLoveInput = (CheckBox) view.findViewById(R.id.editMoodsInLove);
		initCheckBox(moodsInLoveInput, Mood.InLove);
		CheckBox moodsEnergeticInput = (CheckBox) view.findViewById(R.id.editMoodsEnergetic);
		initCheckBox(moodsEnergeticInput, Mood.Energetic);
		CheckBox moodsHappyInput = (CheckBox) view.findViewById(R.id.editMoodsHappy);
		initCheckBox(moodsHappyInput, Mood.Happy);
		CheckBox moodsSadInput = (CheckBox) view.findViewById(R.id.editMoodsSad);
		initCheckBox(moodsSadInput, Mood.Sad);
		CheckBox moodsRelaxedInput = (CheckBox) view.findViewById(R.id.editMoodsRelaxed);
		initCheckBox(moodsRelaxedInput, Mood.Relaxed);
		CheckBox moodsStressedInput = (CheckBox) view.findViewById(R.id.editMoodsStressed);
		initCheckBox(moodsStressedInput, Mood.Stressed);
		
		// times of day
		CheckBox timesOfDayMorningInput = (CheckBox) view.findViewById(R.id.editTimesOfDayMorning);
		initCheckBox(timesOfDayMorningInput, TimeOfDay.Morning);
		CheckBox timesOfDayForenoonInput = (CheckBox) view.findViewById(R.id.editTimesOfDayForenoon);
		initCheckBox(timesOfDayForenoonInput, TimeOfDay.Forenoon);
		CheckBox timesOfDayNoonInput = (CheckBox) view.findViewById(R.id.editTimesOfDayNoon);
		initCheckBox(timesOfDayNoonInput, TimeOfDay.Noon);
		CheckBox timesOfDayAfternoonInput = (CheckBox) view.findViewById(R.id.editTimesOfDayAfternoon);
		initCheckBox(timesOfDayAfternoonInput, TimeOfDay.Afternoon);
		CheckBox timesOfDayEveningInput = (CheckBox) view.findViewById(R.id.editTimesOfDayEvening);
		initCheckBox(timesOfDayEveningInput, TimeOfDay.Evening);
		CheckBox timesOfDayNightInput = (CheckBox) view.findViewById(R.id.editTimesOfDayNight);
		initCheckBox(timesOfDayNightInput, TimeOfDay.Night);
		
		// seasons
		CheckBox seasonsSpringInput = (CheckBox) view.findViewById(R.id.editSeasonsSpring);
		initCheckBox(seasonsSpringInput, Season.Spring);
		CheckBox seasonsSummerInput = (CheckBox) view.findViewById(R.id.editSeasonsSummer);
		initCheckBox(seasonsSummerInput, Season.Summer);
		CheckBox seasonsAutumnInput = (CheckBox) view.findViewById(R.id.editSeasonsAutumn);
		initCheckBox(seasonsAutumnInput, Season.Autumn);
		CheckBox seasonsWinterInput = (CheckBox) view.findViewById(R.id.editSeasonsWinter);
		initCheckBox(seasonsWinterInput, Season.Winter);
		
		// weather conditions
		CheckBox weatherConditionsSunnyInput = (CheckBox) view.findViewById(R.id.editWeatherConditionsSunny);
		initCheckBox(weatherConditionsSunnyInput, Condition.Sunny);
		CheckBox weatherConditionsCloudyInput = (CheckBox) view.findViewById(R.id.editWeatherConditionsCloudy);
		initCheckBox(weatherConditionsCloudyInput, Condition.Cloudy);
		CheckBox weatherConditionsRainyInput = (CheckBox) view.findViewById(R.id.editWeatherConditionsRainy);
		initCheckBox(weatherConditionsRainyInput, Condition.Rainy);
		CheckBox weatherConditionsSnowInput = (CheckBox) view.findViewById(R.id.editWeatherConditionsSnow);
		initCheckBox(weatherConditionsSnowInput, Condition.Snow);
		
		// create the dialog
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setCancelable(false);
		builder.setIcon(android.R.drawable.ic_menu_edit);
		builder.setTitle(track.getTitle());

		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				if (!track.hasContextFactors()) {
					// toast error message
					musicViewInitHandler.sendEmptyMessage(-1);
					return;
				}
				
				// store track with new attributes
				mHandler.updateTrack(track);
				
				// add it to the uploadlist
				mHandler.insertTrackToUploadlist(track.getData(), true);
				
				// reload music view and start uploading service
				musicViewInitHandler.sendEmptyMessage(0);
			}
		});
				
		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// nothing to do
			}
		});
				
		builder.setView(view);	
		setDialog(builder.create());
	}
	
	/**
	 * initializes a checkbox
	 * 
	 * @param checkBox
	 * @param timeOfDay
	 */
	protected void initCheckBox(CheckBox checkBox, final TimeOfDay timeOfDay) {
		checkBox.setText(timeOfDay.toString());
		checkBox.setChecked(timesOfDay.contains(timeOfDay));
		checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					timesOfDay.add(timeOfDay);
				} else {
					timesOfDay.remove(timeOfDay);
				}
			}
		});
	}
	
	/**
	 * initializes a checkbox
	 * 
	 * @param checkBox
	 * @param season
	 */
	protected void initCheckBox(CheckBox checkBox, final Season season) {
		checkBox.setText(season.toString());
		checkBox.setChecked(seasons.contains(season));
		checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					seasons.add(season);
				} else {
					seasons.remove(season);
				}
			}
		});
	}
	
	/**
	 * initializes a checkbox
	 * 
	 * @param checkBox
	 * @param condition
	 */
	protected void initCheckBox(CheckBox checkBox, final Condition condition) {
		checkBox.setText(condition.toString());
		checkBox.setChecked(weatherConditions.contains(condition));
		checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					weatherConditions.add(condition);
				} else {
					weatherConditions.remove(condition);
				}
			}
		});
	}
}
